package com.example.pt.controller;

import com.example.pt.entity.Torrent;
import com.example.pt.dto.ApiResponse;
import com.example.pt.entity.User;
import com.example.pt.service.RecommendationService;
import com.example.pt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/torrents")
public class RecommendationController {

    @Autowired
    private RecommendationService recommendationService;
    @Autowired
    private UserService userService;

//    @GetMapping("/recommend/{user_id}")
//    public ApiResponse<List<Torrent>> getTimeBasedRecommendations(
//            @RequestBody(required = false) Map<String, Object> requestMap) {
//        int limit = requestMap != null && requestMap.containsKey("limit")
//                ? Integer.parseInt(requestMap.get("limit").toString())
//                : 10;
//        List<Torrent> recommendations = recommendationService.getTimeBasedRecommendations(limit);
//        return ApiResponse.success(recommendations);
//    }

    @GetMapping("/recommend/{user_id}")
    public ApiResponse<List<Torrent>> getTimeBasedRecommendations(
            @PathVariable Long user_id,
            @RequestParam(required = false, defaultValue = "10") int limit) {

        List<Torrent> recommendations = recommendationService.getTimeBasedRecommendations(limit);

        User currentUser = userService.findById(user_id);

        float levelMultiplier = 1.0f;
        if (currentUser != null && currentUser.getLevel() != null) {
            switch (currentUser.getLevel()) {
                case "Elite User" -> levelMultiplier = 0.6f;
                case "Power User" -> levelMultiplier = 0.8f;
                default -> levelMultiplier = 1.0f;
            }
        }

        for (Torrent t : recommendations) {
            if (t.getDiscount() == null) {
                t.setDiscount(1.0f);
            }
            t.setDiscount(t.getDiscount() * levelMultiplier);
        }

        return ApiResponse.success(recommendations);
    }

}
